package com.crrcdt.vue_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crrcdt.vue_project.entity.Attribute;
import com.crrcdt.vue_project.entity.Goods;
import com.crrcdt.vue_project.entity.Order;
import com.crrcdt.vue_project.result.Result;
import com.crrcdt.vue_project.result.ResultFactory;
import com.crrcdt.vue_project.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author lj on 2020/10/24.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    //订单数据列表
    @GetMapping("/orders")
    public Result getAllOrders(@RequestParam(name = "query", required = false) String query,
                              @RequestParam("pagenum") Long pagenum,
                              @RequestParam("pagesize") Long pagesize) {
        Page<Order> page = new Page<>(pagenum, pagesize);
        final IPage<Order> ordersIPage;
        if(query!=null){
            ordersIPage = orderService.page(page, new QueryWrapper<Order>().like("order_number", query));
        }else {
             ordersIPage = orderService.page(page, null);
        }
        if(ordersIPage!=null){
            HashMap<Object, Object> data = new HashMap<>();
            data.put("total", ordersIPage.getTotal());
            data.put("pagenum", ordersIPage.getCurrent());
            data.put("goods", ordersIPage.getRecords());
            return ResultFactory.buildSuccessResult(data, "获取成功");
        }
        return ResultFactory.buildFailResult("获取失败！");
    }
    // 修改订单地址
    @PutMapping("/orders/{id}")
    public Result updateOrderById(@PathVariable("id") Integer id,@RequestBody Order order) {
        order.setOrderId(id);
        final boolean res = orderService.updateById(order);
        if(res){
            return ResultFactory.buildSuccessResult(null, "修改成功");
        }
        return ResultFactory.buildFailResult("修改失败！");
    }

}
